package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.ColorOrSizeModel;
import com.tiantianyuegou.model.ProductSizeModel;
import com.tiantianyuegou.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivany on 2016/1/29.
 */
public class ProductSizeAdapter extends BaseAdapter {

    private Context mContext;
    private List<ProductSizeModel> mData;

    public interface IChangeSelcetListener {
        void changeSelect(List<ProductSizeModel> data);
    }

    private IChangeSelcetListener mListener;

    public void setChangeSelectListener(IChangeSelcetListener listener) {
        mListener = listener;
    }

    public ProductSizeAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
    }

    public void setData(List<ProductSizeModel> data) {
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_product_size, parent, false);
            holder = new ViewHolder();
            holder.mGridAdd = (NoScrollGridView) convertView.findViewById(R.id.item_product_size_add_type);
            holder.mGridFixed = (NoScrollGridView) convertView.findViewById(R.id.item_product_size_fixed_type);
            holder.mTxt_type = (TextView) convertView.findViewById(R.id.item_product_size_type);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ProductSizeModel model = mData.get(position);
        if (model.getFixSize() != null && model.getFixSize().size() > 0) {
            final ColorOrSizeGridAdapter mFixAdapter = new ColorOrSizeGridAdapter(mContext, model.getFixSize());
            holder.mGridFixed.setAdapter(mFixAdapter);
            holder.mGridFixed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int p, long id) {
                    boolean isSelect = mData.get(position).getFixSize().get(p).isSelect();
                    mData.get(position).getFixSize().get(p).setIsSelect(!isSelect);
                    mFixAdapter.notifyDataSetChanged();
                    mListener.changeSelect(mData);
                }
            });
        }
        final AddColorOrSizeGridAdapter mAdapter = new AddColorOrSizeGridAdapter(mContext, model.getAddSize());
        holder.mGridAdd.setAdapter(mAdapter);
        mAdapter.setChangeListenr(new AddColorOrSizeGridAdapter.IChangeListener() {
            @Override
            public void deleteData(int p) {
                mData.get(position).getAddSize().remove(p);
                mAdapter.clear();
                for (int i = 0; i <= mData.get(position).getAddSize().size() - 1; i++) {
                    if (mData.get(position).getAddSize().get(i) != null) {
                        mAdapter.addData(mData.get(position).getAddSize().get(i));
                    }
                }
                mAdapter.notifyDataSetChanged();
                mListener.changeSelect(mData);
            }

            @Override
            public void addModel(final EditText editText) {
                editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        String content = editText.getText().toString();
                        if (content != null && !"".equals(content)) {
                            if (actionId == EditorInfo.IME_ACTION_DONE) {
                                ColorOrSizeModel model = new ColorOrSizeModel(editText.getText().toString(), true);
                                mData.get(position).getAddSize().add(model);
                                mAdapter.clear();
                                for (int i = 0; i <= mData.get(position).getAddSize().size() - 1; i++) {
                                    mAdapter.addData(mData.get(position).getAddSize().get(i));
                                }
                                mAdapter.notifyDataSetChanged();
                                mListener.changeSelect(mData);
                            }
                        }
                        return false;
                    }
                });
            }
        });
        holder.mTxt_type.setText(model.getSizeName());
        return convertView;
    }

    class ViewHolder {
        NoScrollGridView mGridFixed;
        NoScrollGridView mGridAdd;
        TextView mTxt_type;
    }
}
